package com.leapp.sk.viewdelegate.leave

import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.WindowBottomDialog
import com.leapp.sk.R
import com.leapp.sk.ui.activity.leave.LeaveEditActivity
import com.leapp.sk.ui.activity.leave.LevelActivity

/**
 * 请假编辑界面视图托管
 * @author  chaohaipeng  on 2018/8/4.
 */
class LeaveEditDelegate :BaseAppDelegate(){
    /**
     * 底部弹出Dialog
     */
    private var windowBottomDialog: WindowBottomDialog? = null
    /**
     * 底部弹出View
     */
    private var bottomView: View? = null

    /**
     * 第一个选项文本
     */
    private var tv_dailog_bottom_frist_text: TextView? = null
    /**
     * 第二个选项文本
     */
    private var tv_dailog_bottom_second_text: TextView? = null
    /**
     * 取消选项文本
     */
    private var tv_dailog_bottom_cancel_text: TextView? = null
    /**
     * 标题
     */
    private var tv_dailog_bottom_title_text: TextView? = null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_leave_edit
    }

    /**
     * 设置开始时期
     */
    fun setStartDate(content:String){
        val tv_ale_start_time_type = getView<TextView>(R.id.tv_ale_start_time_type)
        if (!TextUtils.isEmpty(content)) {
            tv_ale_start_time_type.text = content
        }

    }
    /**
     * 设置结束时期
     */
    fun setEndDate(content:String){
        val tv_ale_end_time_type = getView<TextView>(R.id.tv_ale_end_time_type)
        if (!TextUtils.isEmpty(content)) {
            tv_ale_end_time_type.text = content
        }
    }
    /**
     * 获取类别
     */
    fun getType():String{
        val tv_ale_type = getView<TextView>(R.id.tv_ale_type)
        return tv_ale_type.text.toString()
    }
    /**
     * 获取开始时期
     */
    fun getEndDate():String{
        val tv_ale_end_time_type = getView<TextView>(R.id.tv_ale_end_time_type)
        return tv_ale_end_time_type.text.toString()
    }

    fun contentCheck():Boolean{
        val tv_ale_type = getView<TextView>(R.id.tv_ale_type)
        val tv_ale_start_time_type = getView<TextView>(R.id.tv_ale_start_time_type)
        val tv_ale_end_time_type = getView<TextView>(R.id.tv_ale_end_time_type)
        val et_ale_text = getView<ContainsEmojiEditText>(R.id.et_ale_text)
        val type = tv_ale_type.text.toString()
        val startTime = tv_ale_start_time_type.text.toString()
        val endTime = tv_ale_end_time_type.text.toString()
        val content = et_ale_text.text.toString()
        if (TextUtils.isEmpty(type)){
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_leave_type_title))
            return  false
        }
        if (TextUtils.isEmpty(startTime)){
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_leave_chose_start_time))
            return  false
        }
        if (TextUtils.isEmpty(startTime)){
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_leave_chose_start_time))
            return  false
        }
        if (TextUtils.isEmpty(endTime)){
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_leave_chose_end_time))
            return  false
        }
        if (TextUtils.isEmpty(content)){
            LKToastUtil.showToastShort(getActivity<AppCompatActivity>().resources.getString(R.string.string_leave_input_text))
            return  false
        }

    return true
    }


    /**
     * 获取开始时期
     */
    fun getStartDate():String{
        val tv_ale_start_time_type = getView<TextView>(R.id.tv_ale_start_time_type)
       return tv_ale_start_time_type.text.toString()
    }
    /**
     * 初始化底部弹出
     */
    private  fun initBottomDialog() {
        if (windowBottomDialog == null) {
            windowBottomDialog = WindowBottomDialog(getActivity(), 0)
        }
    }

    /**
     * 初始化底部弹出视图
     */
    private  fun initBottomDialogView() {
        if (bottomView == null) {
            bottomView = View.inflate(getActivity(), R.layout.layout_bottom_dialog, null)
            tv_dailog_bottom_frist_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_frist_text)
            tv_dailog_bottom_second_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_second_text)
            tv_dailog_bottom_cancel_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_cancel_text)
            tv_dailog_bottom_title_text = bottomView?.findViewById<TextView>(R.id.tv_dailog_bottom_title_text)

        }
    }

    /**
     * 初始化请假类型底部弹出窗
     */
    fun initLeaveDialog() {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_leave_event)
        tv_dailog_bottom_second_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_leave_illness)
        tv_dailog_bottom_title_text?.text = getActivity<AppCompatActivity>().resources?.getString(R.string.string_leave_type_title)
        windowBottomDialog?.setBottomView(bottomView)
        windowBottomDialog?.show()
        initEvent()
    }
    /**
     * 初始化事件
     */
   private fun initEvent() {
        initBottomDialog()
        initBottomDialogView()
        tv_dailog_bottom_frist_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_leave_event))
        })
        tv_dailog_bottom_second_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
                setSexData(getActivity<AppCompatActivity>().resources?.getString(R.string.string_leave_illness))
        })
        tv_dailog_bottom_cancel_text?.setOnClickListener(View.OnClickListener {
            windowBottomDialog?.dismiss()
        })
    }
    /**
     * 设置性别数据
     */
    private fun setSexData(content: String?) {
        val tv_ale_type = getView<TextView>(R.id.tv_ale_type)
        if (!TextUtils.isEmpty(content)) {
            tv_ale_type.text = content
        }
    }


    /**
     * 设置标题
     */
    fun setTitle(title:String?){
        val topBar= getView<SKTopBar>(R.id.jtb_ale)
        if (!TextUtils.isEmpty(title)){
            topBar?.setTitle(title)
        }
        setEvent(topBar)
    }
    /**
     * 设置设置事件
     */
    private  fun setEvent(topBar: SKTopBar){
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<LeaveEditActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }
}